package edu.mit.media.fv.aqwidgets
{
	import edu.mit.media.fv.aqview.*;
	import edu.mit.media.fv.events.*;
	import edu.mit.media.fv.fluidspace.*;
	import edu.mit.media.fv.icons.*;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;

	public class AQSubtimeline extends MovieClip
	{
		private var dx:int = 10;
		private var dy:int = 10;
		private var w:int = 525;
		private var h:int = 220;
		private var rows:int = 20;
		private var columns:int = 5;
		private var messageTypes:Array = new Array();
		
		public function AQSubtimeline()
		{
			super();
			this.x = 100;
			this.y = 140;
			this.resetMessageTypes();
			FluidEventHandler.addEventListener(InitializeEvent.INIT, resetMessageTypes);
		}
		public function resetMessageTypes(event:Event = null):void{
			this.messageTypes = [
				new MessageWrapper(FluidConstants.TEXT, new AQTextIcon(), 0, 0xFFFFFF),
				new MessageWrapper(FluidConstants.VOICE, new AQVolumeIcon(), 1, 0xFFFFFF),
				new MessageWrapper(FluidConstants.POLL, new AQPieIcon(), 2, 0xFFFFFF),
				new MessageWrapper(FluidConstants.SHOPPING, new AQShoppingIcon(), 3, 0xFFFFFF),
				new MessageWrapper(FluidConstants.PIX, new AQBrushIcon(), 4, 0xFFFFFF),
				new MessageWrapper(FluidConstants.TIVO, new AQBrushIcon(), 5, 0xFFFFFF),
				];
		}
		public function setMessages(messages:Array):void{
			this.drawMessageCount(messages);
		}
		
		//UI sends:
		//{command: $GET_MESSAGE_COUNT, begin:2173987, end:83249732}
		//engine sends back:
		//{command:$ACK_GET_MESSAGE_COUNT, begin:2173987, end:83249732, messages:{{msg_type:$TXTMSG, count:9},{msg_type:$AUDIOMSG, count:3},{msg_type: $POLL, count:7}}} 
		public function drawMessageCount(messages:Array):void{
			//set up values and arrays needed to properly draw subtimeline
			if(messageTypes.length >0){
				this.dx = w/(this.messageTypes.length);
			}else{
				this.dx = w/(this.messageTypes.length);
			}
			this.dy = h/(rows+2);
			
			this.drawLines(messages);
			this.drawIcons(messages);
			this.drawMessageHistogram(messages);
		}
		public function drawLines(messages:Array):void{
			//draw lines
			this.graphics.lineStyle(2, 0xFFFFFF, 0.50);
			for(var i:int = 1; i<this.messageTypes.length; i++){
				this.graphics.moveTo(this.dx*i, 0);
				this.graphics.lineTo(this.dx*i, this.h);
			}
			this.graphics.moveTo(0, this.h-this.dy*2);
			this.graphics.lineTo(w, this.h-this.dy*2);
		}
		public function drawIcons(messages:Array):void{
			//draw icons
			for(var i:int = 0; i<this.messageTypes.length; i++){
				var message:MessageWrapper = MessageWrapper(this.messageTypes[i]);
				var icon:DisplayObject = message.icon;
				icon.width = this.dy*1.75;
				icon.height = this.dy*1.75;
				icon.x = (i+0.5)*this.dx;
				icon.y = this.h - this.dy;
				this.addChild(icon);
			}
		}
		public function drawMessageHistogram(messages:Array):void{
			//draw messages
			for(var i:int = 0; i<messages.length; i++){
				var message:MessageWrapper = this.getMessageType(messages[i].msg_type);
				this.graphics.beginFill(message.color);
				for(var j:int = 0; j<messages[i].count; j++){
					var x:int = message.index*this.dx+((j%this.columns)+1)*this.dx/(this.columns+1)
					var y:int = this.h - this.dy*(3+Math.floor(j/this.columns));
					var r:int = Math.min(this.dx/(this.columns+1), this.dy)/2;
					this.graphics.drawCircle(x, y, r);
				}
				this.graphics.endFill();
			}
		}
		public function getMessageType(type:Object):MessageWrapper{
			for(var i:int = 0; i<this.messageTypes.length; i++){
				var message:MessageWrapper = MessageWrapper(this.messageTypes[i]);
				if (message.type == type){
					return message;
				}
			}
			trace("no message wrapper found for "+type);
			return null;
		}
	}
}